package com.ai.bitcoin.jpa.service.impl;

import com.ai.bitcoin.bo.BccNewsTop5BO;
import com.ai.bitcoin.constant.CacheConstant;
import com.ai.bitcoin.constant.CommonConstant;
import com.ai.bitcoin.jpa.dao.BccNewsDao;
import com.ai.bitcoin.jpa.dao.CoinNews2BccDao;
import com.ai.bitcoin.jpa.entity.BccNews;
import com.ai.bitcoin.jpa.entity.CoinNews2Bcc;
import com.ai.bitcoin.jpa.service.BccNewsService;
import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.util.DateUtil;
import com.ai.bitcoin.vo.BccNewsTop5VO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Jason
 * @date 2018.5.13
 **/
@Component
@Slf4j
public class BccNewsServiceImpl implements BccNewsService {

    @Autowired
    private BccNewsDao dao;

    @Autowired
    private CoinNews2BccDao coinNews2BccDao;

    @Autowired
    private BccRedisService bccRedisService;

    @Override
    public List<BccNews> findByBatchNo(String batchNo) {
        return dao.findByBatchNo(batchNo);
    }

    @Override
    public List<BccNews> findByBatchNo(String batchNo, Pageable pageable) {
        return dao.findByBatchNo(batchNo, pageable);
    }

    @Override
    public List<BccNews> findAll(Pageable pageable) {
        Page<BccNews> page = dao.findAll(pageable);
        return page.getContent();
    }

    @Override
    public List<BccNews> queryCurrentNewsByBccId(String bccId) {
        String key = CacheConstant.BITCOIN_BCC_NEWS + DateUtil.currentDate2Str(CommonConstant.REPORT_HTML_PREFIX_PATH) + "_" + bccId;
        try {
            List<BccNews> cacheBitCoinBccNewsList = bccRedisService.getBitCoinBccNews(key);
            if (cacheBitCoinBccNewsList != null && cacheBitCoinBccNewsList.size() > 0) {
                return cacheBitCoinBccNewsList;
            }
            List<BccNews> bccNewsList = dao.queryCurrentNewsByBccId(bccId);
            bccRedisService.cache2BitCoinBccNews(bccId, bccNewsList);
            return bccNewsList;
        } catch (Exception e) {
            log.info("【比特币的舆情放入缓存异常】： {}", e.getMessage());
        }
        return Lists.newArrayList();
    }

    @Override
    @Cacheable("BccNews")
    public BccNews queryByNewsId(String newsId) {
        Optional<BccNews> bccNews = dao.findById(newsId);
        if (bccNews.isPresent()) {
            return bccNews.get();
        }
        return null;
    }

    @Override
    public BccNewsTop5VO getTop3BccNewsBccId(BccNewsTop5BO newsTop5BO) {
        BccNewsTop5VO bccNewsTop5VO = new BccNewsTop5VO();

        List<CoinNews2Bcc> top5ByBccId = coinNews2BccDao.getTop3ByBccId(newsTop5BO.getBccId());
        if (null == top5ByBccId || top5ByBccId.isEmpty()) {
            log.warn("根据bccId:{},查出的对应的舆情Id为空", newsTop5BO.getBccId());
            return bccNewsTop5VO;
        }

        List<BccNews> bccNewsList = top5ByBccId.stream().map(CoinNews2Bcc::getNews).collect(Collectors.toList());
        bccNewsTop5VO.setBccNewsList(bccNewsList);

        return bccNewsTop5VO;
    }

}
